﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ManagerServices.Entity;
using ManagerServices.Constants;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data.Common;
using System.Data;

namespace ManagerServices.Data
{
    public class UserData
    {
        private static UserData instance = null;

        public static UserData Instance
        {
            get
            {
                if (instance == null)
                    instance = new UserData();

                return instance;
            }

        }

        public int CreateUser(UserEntity userinfo)
        {
            
            
            //string sqlCommand = "SELECT TOP 5 FirstName, LastName, JobTitle FROM HumanResources.vEmployee"; 
            //_database.GetSqlStringCommand(sqlCommand); 
            int filasEfectadas = 0;
            Database _database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _database.GetStoredProcCommand("sp_add_user");
            try
            {
                
            _database.AddInParameter(dbCommand,"@MAIL",System.Data.DbType.String, userinfo.MAIL  );
            _database.AddInParameter(dbCommand, "@PASSWORD", System.Data.DbType.String, userinfo.PASSWORD);
            _database.AddInParameter(dbCommand, "@DATECREATE", System.Data.DbType.DateTime, userinfo.DATECREATE);
            _database.AddInParameter(dbCommand, "@DATEUPDATE", System.Data.DbType.DateTime, userinfo.DATEUPDATE);
            _database.AddInParameter(dbCommand, "@ID_PROFILE", System.Data.DbType.Int32, userinfo.ID_PROFILE);

            filasEfectadas = _database.ExecuteNonQuery(dbCommand);

            
            }
                catch(Exception e)
            {
                
                    filasEfectadas = 0;
                
             }
            finally
            {
                dbCommand.Dispose();
                
            }
            
           
             return filasEfectadas ;
            
        }

        public bool ValidationMail(String mail)
        {
            int filasEfectadas = 0;
            bool bresultvalidation = false;
            Database _database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = _database.GetSqlStringCommand("select Mail from TBL_USER where MAIL = @MAil");

            try
            {

                _database.AddInParameter(dbCommand, "@MAIL", System.Data.DbType.String, mail);
                filasEfectadas = _database.ExecuteNonQuery(dbCommand);
                if (filasEfectadas > 0)
                    bresultvalidation = true;
            }
            catch (Exception e) 
            { 
                bresultvalidation = false;
            }

            return bresultvalidation;
            
        }

        //public int GetUser(int idprofile)
        //{

        //    Database _database = DatabaseFactory.CreateDatabase(Dbconfig.Setting);
        //    string sqlCommand = "SELECT TOP 5 FirstName, LastName, JobTitle FROM HumanResources.vEmployee";
        //    DbCommand dbCommand = _database.GetSqlStringCommand(sqlCommand);

        //    using (IDataReader _dataReader = _database.ExecuteReader(dbCommand))
        //    {

        //        while (_dataReader.Read())
        //        {


        //            Console.WriteLine(string.Format("{0}, {1}, {2}", _dataReader["FirstName"],
        //                    _dataReader["LastName"], _dataReader["JobTitle"]));
        //        }

        //        if (_dataReader != null)
        //            _dataReader.Close();
        //    }
        //}
    }
}